package com.example.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.auth.entity.SysUser;
import com.example.auth.entity.dto.LoginDTO;
import com.example.auth.entity.vo.RouterVO;
import com.example.auth.exception.LyException;
import com.example.auth.mapper.SysUserMapper;
import com.example.auth.service.LoginService;
import com.example.auth.service.SysMenuService;
import com.example.auth.utils.JwtHelper;
import com.example.auth.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: LiYang
 * @create: 2023-07-11 16:25
 * @Description:
 * @Version 1.0
 **/
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private RedisUtil redisUtil;
    private static String TOKEN = "Access-Token";

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private AuthenticationManager authenticationManager;
    @Value("${user.roles}")
    private String roles;
    @Value("${user.avatar}")
    private String avatar;
    @Override
    public Map<String, Object> login(LoginDTO loginDTO) {
        try {
            Authentication authenticationToken = new UsernamePasswordAuthenticationToken(loginDTO.getUsername(), loginDTO.getPassword());
            authenticationManager.authenticate(authenticationToken);
        } catch (AuthenticationException e) {
            throw new LyException(e.getMessage());
        }
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, loginDTO.getUsername());
        SysUser sysUser = sysUserMapper.selectOne(wrapper);
        String token = JwtHelper.createToken(sysUser.getId(), sysUser.getUsername());
        redisUtil.set(token,sysUser,30 * 60);
        Map<String, Object> map = new HashMap<>();
        map.put("token",token);
        return map;
    }

    @Override
    public Map<String, Object> getInfo(HttpServletRequest request) {
        var token = request.getHeader(TOKEN);
        var userId = JwtHelper.getUserId(token);
        String username = JwtHelper.getUsername(token);
        Map<String, Object> map = new HashMap<>();
        map.put("roles",roles);
        map.put("avatar",avatar);
        map.put("name",username);
        List<RouterVO> routers = sysMenuService.getUserMenuList(userId);
        List<String> buttons = sysMenuService.getUserPermsList(userId);
        //菜单权限
        map.put("routers",routers);
        //按钮权限
        map.put("buttons",buttons);
        return map;
    }
}
